/*
 * 作者:熊猫
 * 日期:2017 - 3 - 15 10 - 41 - 42
 *
 */

package net.lainiao.morethread.Chapter7.demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/15.
 */
public class Main {
    public static void main(String[] args) {
        MyExecutor myExecutor=new MyExecutor(2,4,1000, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>());
        List<Future<String>> results=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            SleepTwoSecondTask task=new SleepTwoSecondTask();
            Future<String> result=myExecutor.submit(task);
            results.add(result);
        }
        for (int i = 0; i < 5; i++) {
            try {
                Future<String> future=results.get(i);
                String result=future.get();
                System.out.println("Main Result for Task "+i+" value "+result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        myExecutor.shutdown();
        for (int i = 5; i < 10; i++) {
            try {
                Future<String> future=results.get(i);
                String result=future.get();
                System.out.println("Main Result for Task "+i+" value "+result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        try {
            myExecutor.awaitTermination(1,TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main Over");
    }
}
